package com.winchain.chainbill.risk.biz.access.rx;

import com.shell.bee.base.utils.SystemUtil;
import com.shell.framework.command.ChainBase;
import com.shell.framework.command.CommandContext;
import com.shell.framework.rx.OnErrorNotImplementedSubscriber;
import com.winchain.chainbill.basic.context.TimeoutOperation;
import com.winchain.chainbill.chain.callback.cmd.SyncConfirmCmd;
import com.winchain.chainbill.receivables.base.po.ArOperation;
import com.winchain.chainbill.receivables.base.po.ArRecord;
import com.winchain.chainbill.receivables.base.service.ArRecordService;
import com.winchain.chainbill.risk.base.dao.RmAccessQuotaDao;
import com.winchain.chainbill.risk.base.po.RmAccess;
import com.winchain.chainbill.risk.base.po.RmAccessArRecord;
import com.winchain.chainbill.risk.base.po.RmAccessQuota;
import com.winchain.chainbill.risk.base.service.RmAccessArRecordService;
import com.winchain.chainbill.risk.biz.access.cmd.AccessModCmd;
import com.winchain.chainbill.risk.biz.access.cmd.AccessMoveCmd;
import com.winchain.chainbill.risk.biz.access.cmd.AccessQuotaCmd;
import com.winchain.chainbill.risk.biz.access.cmd.AccessTranCmd;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ReceivablesCancSubscriber extends OnErrorNotImplementedSubscriber<BatchReceivablesCancEvent> {
	@Autowired
	private ArRecordService arRecordService;
	@Autowired
	private RmAccessQuotaDao rmAccessQuotaDao;
	@Autowired
	private RmAccessArRecordService rmAccessArRecordService;
	@Autowired
	private AccessModCmd accessModCmd;
	@Autowired
	private AccessMoveCmd accessMoveCmd;
	@Autowired
	private AccessQuotaCmd accessQuotaCmd;
	@Autowired
	private AccessTranCmd accessTranCmd;
	@Autowired
	private SyncConfirmCmd<RmAccessQuota> syncConfirmCmd;

	public void onApplicationEvent(BatchReceivablesCancEvent t) {
		RmAccess rmAccess = t.getRmAccess();

		CommandContext<RmAccessQuota> context = new CommandContext();

		RmAccessQuota rmAccessQuota = new RmAccessQuota();
		rmAccessQuota.setAccId(SystemUtil.getUUID());

		BigDecimal amount = new BigDecimal(0.0D);

		List<String> buyerIds = new ArrayList();
		buyerIds.add(rmAccess.getBuyerId());
		List<ArRecord> arRecords = this.arRecordService.selectRecordBySellerIdAndBuyerIds(rmAccess.getSellerId(),
				buyerIds);
		for (ArRecord arRecoid : arRecords) {
			RmAccessArRecord rmAccessArRecord = new RmAccessArRecord();
			rmAccessArRecord.setArId(arRecoid.getArId());
			rmAccessArRecord.setArStateMod(arRecoid.getArState());
			rmAccessArRecord.setAccId(rmAccessQuota.getAccId());
			rmAccessArRecord.setRmarId(SystemUtil.getUUID());

			this.rmAccessArRecordService.save(rmAccessArRecord);

			amount = amount.add(arRecoid.getCreditMoney());
		}
		rmAccessQuota.setBuyerId(rmAccess.getBuyerId());
		rmAccessQuota.setSellerId(rmAccess.getSellerId());
		rmAccessQuota.setArUpperLimMod(rmAccess.getArUpperLim());
		rmAccessQuota.setConvertRatioMod(rmAccess.getConvertRatio());
		rmAccessQuota.setOutputAvlMod(rmAccess.getOutputAvl());
		rmAccessQuota.setLoanAvlMod(rmAccess.getLoanAvl());
		rmAccessQuota.setOptSeq(Integer.valueOf(0));
		rmAccessQuota.setAmount(amount);
		this.rmAccessQuotaDao.insert(rmAccessQuota);

		ArOperation opt = t.getOperation();
		TimeoutOperation timeoutOpt = t.getTimeoutOperation();

		opt.setNextState("M");
		opt.setSeqNo(rmAccessQuota.getOptSeq());
		context.setSource(rmAccessQuota);
		context.setValue("Operation", opt);
		context.setValue("TimeoutOperation", timeoutOpt);

		ChainBase<RmAccessQuota> crb = new ChainBase();
		crb.add(this.accessModCmd);
		crb.add(this.accessMoveCmd);
		crb.add(this.accessQuotaCmd);
		crb.add(this.accessTranCmd);
		crb.add(this.syncConfirmCmd);
		crb.execute(context);
	}
}
